/**********************************************************************
 *	Custom input types for the jquery.jeditable plugin
 * By Richard Davies <Richard__at__richarddavies.us>
 *********************************************************************/

// Create a custom input type for date fields
$.editable.dateTypeCount = 0;
$.editable.addInputType("date", {
	element : function(settings, original) {
		var param = {count: ++$.editable.dateTypeCount};
		var selectMonth = $('<select id="date{count}-mm" name="date-mm">'.supplant(param));                
		var selectDay = $('<select id="date{count}-dd" name="date-dd">'.supplant(param));                
		var selectYear = $('<select id="date{count}" name="date">'.supplant(param));                
		var defaultOption = $('<option value=""></option>');
		
		// Add month option values
		$(defaultOption).clone().appendTo(selectMonth);
		for (var i = 1; i <= 12; i++) {
			// Convert numeric month into 3 letter abbreviation (i.e. "Feb")
			var month = new Date(2000, i-1).toString().substr(4, 3);
			var param = {i: i, month: month};
			var option = $('<option value="{i}">{month}</option>'.supplant(param));
			$(selectMonth).append(option);
		}
		
		// Add day option values
		$(defaultOption).clone().appendTo(selectDay);
		for (var i = 1; i <= 31; i++) {
			var param = {i: i};
			var option = $('<option value="{i}">{i}</option>'.supplant(param));
			$(selectDay).append(option);
		}
		
		// Add year option values
		$(defaultOption).clone().appendTo(selectYear);
		var year = new Date().getFullYear();
		for (var i = year; i >= 1980 ; i--) {
			var param = {i: i};
			var option = $('<option value="{i}">{i}</option>'.supplant(param));
			$(selectYear).append(option);
		}
		
		// Put 'em all together
		$(this).append(selectMonth).append(" ");
		$(this).append(selectDay).append(" ");
		$(this).append(selectYear);

		// Create hidden input that will contain the value to be submitted
		var input = $('<input type="hidden">');                
		$(this).append(input);
		return(input);
	},
	content : function(string, settings, original) {
		// Set "selected" attribute of correct option
		var date = new Date(string);
		var param = {month: date.getMonth()+1, day: date.getDate(), year: date.getFullYear()};
		var monthOption = $('[name=date-mm] [value={month}]'.supplant(param), this);
		var dayOption = $('[name=date-dd] [value={day}]'.supplant(param), this);
		var yearOption = $('[name=date] [value={year}]'.supplant(param), this);
		$(monthOption).attr("selected", "selected");
		$(dayOption).attr("selected", "selected");
		$(yearOption).attr("selected", "selected");
	},
	submit : function(settings, original) {
		// Transfer date from selects to hidden input
		var selectMonth = $('[name=date-mm]', this);
		var selectDay = $('[name=date-dd]', this);
		var selectYear = $('[name=date]', this);
		var date = selectYear.val() + "-"+selectDay.val() + "-"+selectMonth.val() ;
		if (date == "--")
			date = "";
		$('input', this).val(date);
	}
});

		
// Variable substitution function - http://javascript.crockford.com/remedial.html
String.prototype.supplant = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};
